import request from '@/service/request'
import qs from 'qs'
import * as api from "./api";
import { toastMsg } from '@/lib/toast'

export interface RequesParams {
  url: string;
  data?: Object;
}

export function postOrPutBody(params: RequesParams, method: 'post' | 'put') {
  return request({
    url: params.url,
    method,
    data: params.data,
  })
}

// 请求体接收
export function requestBody(params: RequesParams) {
  return request({
    url: params.url,
    method: 'post',
    data: params.data,
  })
}

export function requestParmas(params: RequesParams) {
  return request({
    url: params.url,
    method: 'post',
    data: params.data,
    transformRequest: request.transformRequest,
    headers: request.headers,
  })
}

// 上传文件
export function uploadMultiFile(params: RequesParams) {
  return request({
    url: params.url,
    method: 'post',
    data: params.data,
    headers: { 'Content-Type': 'multipart/form-data' },
  })
}

export function getBySerializer(params: RequesParams) {
  return request({
    url: params.url,
    method: 'get',
    params: params.data,
    paramsSerializer: function (params) {
      return qs.stringify(params, { arrayFormat: 'repeat' })
    },
  })
}

export function getObject(params: RequesParams) {
  return request({
    url: params.url,
    method: 'get',
    params: params.data,
  })
}

export function getObjectById(params: RequesParams, id: string | number) {
  return request({
    url: params.url + '/' + id,
    method: 'get',
    params: params.data,
  })
}

export function updateBody(params: RequesParams) {
  return request({
    url: params.url,
    method: 'put',
    data: params.data,
    // headers: { 'Content-Type': 'application/json;charset=utf-8' }
  })
}

export function updateBodyById(params: RequesParams, id: string | number) {
  return request({
    url: params.url + '/' + id,
    method: 'put',
    data: params.data,
    // headers: { 'Content-Type': 'application/json;charset=utf-8' }
  })
}

export function deleteObjectById(params: RequesParams, id: string | number) {
  return request({
    url: params.url + '/' + id,
    method: 'delete',
    data: params.data,
    // headers: { 'Content-Type': 'application/json;charset=utf-8' }
  })
}

export function deleteObject(params: RequesParams) {
  return request({
    url: params.url,
    method: 'delete',
    data: params.data,
    // headers: { 'Content-Type': 'application/json;charset=utf-8' }
  })
}

export function deleteParmas(params: RequesParams) {
  return request({
    url: params.url,
    method: 'delete',
    params: params.data,
    paramsSerializer: (params) => {
      return qs.stringify(params, { indices: false })
    },
  })
}

// 请求体接收
export function requestBodyG(params: RequesParams) {
  return request({
    url: params.url,
    method: 'post',
    data: params.data,
    // headers: { 'Content-Type': 'application/json;charset=utf-8' }
  })
}

export function getObjectListG(params: RequesParams) {
  return request({
    url: params.url,
    method: 'get',
    params: params.data,
  })
}

export function getObjectList(params: RequesParams) {
  return request({
    url: params.url,
    method: 'get',
    params: params,
  })
}


export function downloadBlob(params: RequesParams, filename?: string | null) {
  request({
    url: params.url,
    method: 'get',
    params: params.data,
    responseType: 'blob'
  }).then(response => {
    // 创建Blob链接
    const url = window.URL.createObjectURL(new Blob([response.data]));

    // 创建a标签，模拟点击下载
    const link = document.createElement('a');
    link.href = url;

    // 设置下载文件名
    // 可以从后端响应头Content-Disposition中提取文件名，这里写死为apiinfo.zip
    if (!filename) {
      const disposition = response.headers['content-disposition'];
      if (disposition) {
        const filenameMatch = disposition.match(/filename="?([^"]+)"?/);
        if (filenameMatch.length === 2) filename = filenameMatch[1];
      }
    }

    link.setAttribute('download', filename || '未知文件');

    document.body.appendChild(link);
    link.click();

    // 释放资源
    link.remove();
    window.URL.revokeObjectURL(url);
  }).catch(error => {
    toastMsg.error("下载错误")
  })
}


export const apis = api
export const toast = toastMsg